const LOTTERQRCODE = WEB_HOME+'/'+APP_SERVICE+'/lottery/lottery.html?'  
const TOOLBAR =[
	{content:"添加", icon:"el-icon-document-add",type:"primary"}
]
const COLUMNS = [
	{prop:"createdate", label:"创建日期",width:"100"},
	{prop:"name", label:"抽奖人姓名",width:"100"},
	{prop:"tel", label:"手机号码",width:"150"},
	{prop:"company", label:"单位名称",width:"150"},
	{prop:"number", label:"抽奖次数",width:"80"},
	{prop:"lotterynumber", label:"已抽奖次数",width:"80"},
	{prop:"status", label:"状态",width:"80"},
	{prop:"lotterydate", label:"抽奖日期",width:"100"},
	{prop:"lotteryresult", label:"抽奖结果",width:"180"},
	{prop:"memo", label:"备注",width:"80"},
]

const lotteryRules = {
    name: [
        {required: true, message: '请输入抽奖人姓名', trigger: 'blur'}
    ],
	number: [
	    {required: true, message: '请输入抽奖次数', trigger: 'blur'}
	],
	tel: [
		{required: true, message: '请输入手机号码', trigger: 'blur'}
	],
	company: [
		{required: true, message: '请输入单位名称', trigger: 'blur'}
	]
};
			
let app = new Vue({
    el: "#app",
    data() {
        return {
        	leftbar_show:true,
			// 添加、编辑标识
			tableHeight:window.innerHeight - 180,
			toolbar:TOOLBAR,
			tablecolums:COLUMNS,
			dialogFormVisible:false,
			dialogbatchFormVisible:false,
			QR:'',
			queryForm:{
				userid: localStorage.getItem('USERID'),
                keyword: "",
				status:[],
				date:[],
				pageNo:1, // 当前页码
				pagesize:100 ,// 每页记录数
				pagesizes:[10,20,50,100,500]
				
			},
			pagetotal:0,
			pickerOptions: {
			  shortcuts: [{
				text: '最近一周',
				onClick(picker) {
				  const end = new Date()
				  const start = new Date()
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
				  picker.$emit('pick', [start, end])
				}
			  }, {
				text: '最近一个月',
				onClick(picker) {
				  const end = new Date();
				  const start = new Date();
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
				  picker.$emit('pick', [start, end]);
				}
			  }, {
				text: '最近三个月',
				onClick(picker) {
				  const end = new Date();
				  const start = new Date();
				  start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
				  picker.$emit('pick', [start, end]);
				}
			  }]
			},
			lotteryRules:lotteryRules, // 数据验证规则
			lotteryForm: {
				id:null,
                name: "",
				number: 0,
				tel: "",
				company: "",
				memo: "",
				presserid:localStorage.getItem('USERID')
            },
			// 多选
			multipleSelection:[],
			pagedata:[],  // 当前页结果集
			loaded:false,
        }
    },
	methods: {
		// 显示侧边栏
		showLeftBar() {
			if (this.leftbar_show) {
				this.leftbar_show = false
			} else {
				this.leftbar_show = true
			}
		},
		createQRcode(index, row) {
			document.getElementById(index).innerHTML = ''
			var qrcode = new QRCode(document.getElementById(index), {
				width: 100,
				height: 100
			});
			var href = LOTTERQRCODE + "id=" + row.id + "&token=" + localStorage.getItem('TOKEN')
			qrcode.makeCode(href);
		},
		
		// 表格多选
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		selectionChange(selection){
			this.selection = selection
		},
		toolAction(acName) {
			if (acName == "添加") {
				this.add()
			} 
		},
		// 确认
		async cofirm(){
            this.$refs.lotteryForm.validate(async valid => {
                if (valid){
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = null;
						if (this.action=="add") {
							res = await axios.put('api/lottery/add', this.lotteryForm);
						} else{
							// 编辑
							res = await axios.put('api/lottery/update', this.lotteryForm);
						}
						if (res.code === 200) {
							this.query();
							this.dialogFormVisible = false
						}else {
							this.$message.error(res.message);
						}		
					}catch (e) {
						this.$message.error('添加或编辑请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
                }
            })			
		},
		start(){
			this.queryForm.pageNo = 1
			this.query()
		},
		// 查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/lottery/query', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					this.$message.success(res.message);
				}else {
					this.$message.error(res.message);
				}
			}catch (e) {
				this.$message.error('查询请求出错！', e.message);
			}
			
			// 关闭加载框
			loading.close();
		},
		// 添加
		add() {
			this.action="add";
			this.dialogFormVisible = true;
		    this.lotteryForm= {
				id:null,
		        name: "",
				number: 0,
		    	tel: "",
		    	company: "",
		    	memo: "",
		    	presserid:localStorage.getItem('USERID')
		    }
		},
		// 编辑
		edit(index, row) {
			this.action="edit";
			this.index = index;
			this.dialogFormVisible = true;
			this.lotteryForm = deepCopy(row)
		},
		// 设定表格当前页
		handleCurrentPageChange (selectedPageNo) {
		  this.queryForm.pageNo = selectedPageNo
		  // 加载数据页
		  this.query()
		},
	},
	async mounted() {
		// 自动调整表格高度
		window.onresize = () => {
		  return (() => {
			this.tableHeight = window.innerHeight - 180
		  })()
		}
	
	}

});	